package 家庭收支系统;

import java.util.Scanner;


class FamilyAccount {
    public static void main(String[] args) {

        int money = 10000;
        int a=money;
        String details = "\n---------------------------\n"+"\t\n收    入:"+"\t\n账户金额:"+money+"\t\n收支金额:"+"\t\n说    明:"+"\n---------------------------\n";

        boolean loopFlag = true;
        do {
            System.out.println("\n----------------------------\n" +
                    "         家庭收支系统\n" +
                    "----------------------------\n" +
                    "         1.收支明细\n" +
                    "         2.登记收入\n" +
                    "         3.登记支出\n" +
                    "         4.退    出\n" +
                    "         请选择1-4");

            char key = Main.readMenuSelection();
            System.out.println();
            switch (key) {
                case '1':
                    System.out.print("当前收支明细记录");
                    System.out.print(details);
                    break;
                case '2':
                    System.out.print("本次收入金额：");
                    int amount1 = Main.readNumber();
                    System.out.print("本次收入说明：");
                    String desc1 = Main.readString();
                    money += amount1;
                    if (money>a){
                    details = "\t\n收    入：" + amount1 + "\t\n账户金额："
                            + money + "\t\n收支金额："+ amount1 + "\t\n说    明：" + desc1 + "\n";
                    } else {
                        details = "\t\n收    入：" + amount1 + "\t\n账户金额："
                                + money + "\t\n收支金额： 无收入" + "\t\n说    明：" + desc1 + "\n";
                    }
                    System.out.print("登记完成");
                    break;
                case '3':
                    System.out.print("本次支出金额：");
                    int amount2 = Main.readNumber();
                    System.out.print("本次支出说明：");
                    String desc2 = Main.readString();
                    money -= amount2;
                    if(money<a){
                    details = "\t\n支     出：" + amount2 + "\t\n账户金额："
                            + money + "\t\n支出金额："+amount2 + "\t\n说    明：" + desc2 + "\n";
                    }else {
                        details = "\t\n支     出：" + amount2 + "\t\n账户金额："
                                + money + "\t\n支出金额： 无支出金额 "+ "\t\n说    明：" + desc2 + "\n";
                    }
                    System.out.print("登记完成");
                    break;
                case '4':
                    System.out.print("确认是否退出？（Y/N）");
                    char yn = Main.readConfirmSelection();
                    if (yn == 'Y') loopFlag = false;
                    break;
            }
        } while (loopFlag);
    }
}

public class Main {
    public static Scanner sca = new Scanner(System.in);

    public static char readMenuSelection() {
        char i;
        for (; ; ) {
            String s = readKeyBoard(1,false);
            i = s.charAt(0);
            if (i != '1' && i != '2' && i != '3' && i != '4') {
                System.out.print("输入错误，请重新输入：");
            } else break;
        }
        return i;
    }

    public static char readConfirmSelection() {
        char i;
        for (; ; ) {
            String s = readKeyBoard(1,false).toUpperCase();
            i = s.charAt(0);
            if (i == 'Y' || i == 'N') {
                break;
            } else {
                System.out.print("选择错误，请重新输入：");
            }

        }
        return i;
    }

    public static String readKeyBoard(int limit,boolean blankReturn) {
        String line = "";
        while (sca.hasNextLine()) {
            line = sca.nextLine();
            if (line.length() == 0) {
                if (blankReturn) return line;
                else continue;
            }
            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度（不大于" + limit + "）错误，请重新输入：");
                continue;
            }break;
        }return line;
    }



    public static int readNumber() {
        int n;
        for (; ; ) {
            String s = readKeyBoard(4,false);
            try {
                n = Integer.parseInt(s);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static String readString() {
        String s = readKeyBoard(8,false);
        return s;
    }
}





